package com.panshi.service.impl;

import com.panshi.bean.*;
import com.panshi.mapper.AppUserMapper;
import com.panshi.service.AppUserService;
import com.panshi.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppUserServiceImpl implements AppUserService {

    @Resource
    private AppUserMapper appUserMapper;

    @Override
    @Cacheable(value = "hasbei:userInfo", key="userId")
    public AppUser queryUserInfo(Integer userId) {
        return appUserMapper.queryUserInfo(userId);
    }

    @Override
    public List<Dynamic> queryUserDynamic(PageBean pageBean) {

        List<Dynamic> list = appUserMapper.queryUserDynamic(pageBean);

        for (Dynamic dynamic: list) {

            if ("1".equals(dynamic.getDynamicType())) {

                Essay e = appUserMapper.queryEssayById(dynamic.getDynamicId());

                dynamic.setData(e);
            }else if ("3".equals(dynamic.getDynamicType())) {

                Question question = appUserMapper.queryQuestionById(dynamic.getDynamicId());

                dynamic.setData(question);
            }else if ("2".equals(dynamic.getDynamicType())) {

                Map map = new HashMap();

                Answer answer = appUserMapper.queryAnswerById(dynamic.getDynamicId());

                Question question = appUserMapper.queryQuestionById(answer.getMatterId());
                map.put("answer",answer);
                map.put("question",question);


                dynamic.setData(map);
            }

        }

        return list;
    }

 /*   @Override
    public Essay queryEssayById(String id) {
        return null;
    }

    @Override
    public Question queryQuestionByid(String id) {
        return null;
    }

    @Override
    public Answer queryAnswerById(String id) {
        return null;
    }*/
}
